home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Why does this work?
- Date: 20 Feb 1996 11:36:39 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gd7s7INNnjr@keats.ugrad.cs.ubc.ca>
- References: <4g8f7o$adt@ncar.ucar.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4g8f7o$adt@ncar.ucar.edu>,
- Jim Rosinski <rosinski@ra.cgd.ucar.edu> wrote:
- >
- >Could someone please explain why the following code works? In particular, I
- >am perplexed as to why "sub1" and "sub2" declared as elements of "cmndtable"
- >are valid "pointers to function returning void" (i.e. see the definition of
- >"cmndstruct"). When executed, the net result is indeed to invoke two
- >functions, "sub1", and "sub2" (tested on Solaris, AIX, and UNICOS).
-
- It works because the initializers are just assigned to whatever available
- fields there are in the structure. I guess what you are asking is why you
- weren't forced to write the initializer as { { sub1 }, { sub 2 }, { NULL } } to
- indicate that the elements are part of the structure, right?
-
- Your usage might not be standard, even though it's silently accepted by three
- compilers you know. K&R2 says, on p. 219, that
-
- "The initializer for a structure is is either an expression of
- the same type, or a brace-enclosed list of initializers for its
- members in order. Unnamed bit-field members are ignored, and
- are not initialized. If there are fewer initializers in the
- list than members of the structure, the trailing members are
- initialized with 0. There may not be more initializers than
- members."
-
- So far, this would indicate that your code is not compliant, unless the
- function pointer's type is compatible with a single-membered structure
- containing a function pointer (not true).
-
- However, a few paragraphs later, the K&R talks about more rules regarding
- aggregate initializers:
-
- "An _aggregate_ is a structure or array. If an aggregate contains
- members of aggregate type, the initialization rules apply recursively.
- Braces may be elided in the initialization as follows: if the
- initializer for an aggregate's member that is itself an aggregate
- begins with a left brace, the the succeeding comma-separated list of
- initializers initializes members of the subaggregate; it is erroneous
- *> for there to be more initializers than members. If, however, the
- *> initializer for a subaggregate does not begin with a left brace, then
- *> only enough elements from the list are taken to account for the the
- *> members of the subaggregate; any remaining members are left to
- *> initialize the next member of the aggregate of which the subaggregate
- *> is part.
-
- The last part refers precisely to your construct. You did not begin the
- structure initializers with a left brace, hence the members of the initializer
- list are simply handed to the next available fields in the next member of the
- outer aggregate (i.e. next structure in the array).
-
- Of course, K&R2 is _not_ a subsititue for the ANSI/ISO standard, but it's the
- best I can do. Dan Pop, the keeper of the standard, might have some additional
- pearls of wisdom to add.
-
- [ code omitted ]
- --
-
-